home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / StreamerPost.cs116 < prev    next >
Text File  |  2006-09-16  |  7KB  |  205 lines

  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Web;
  5. using System.Net;
  6. using System.Collections.Specialized;
  7.  
  8. /// <summary>
  9. /// This class is used to ease the process of posting commands to VLC that is running on the server.  It can
  10. /// be reused for anything that needs to post to a different website
  11. /// </summary>
  12. /// 
  13. namespace gbweb.classes
  14. {
  15.     public class StreamerPost
  16.     {
  17.         /// <summary>
  18.         /// determines what type of post to perform.
  19.         /// </summary>
  20.         public enum PostTypeEnum
  21.         {
  22.             /// <summary>
  23.             /// Does a get against the source.
  24.             /// </summary>
  25.             Get,
  26.             /// <summary>
  27.             /// Does a post against the source.
  28.             /// </summary>
  29.             Post
  30.         }
  31.  
  32.         private string m_url = string.Empty;
  33.         private NameValueCollection m_values = new NameValueCollection();
  34.         private PostTypeEnum m_type = PostTypeEnum.Get;
  35.         /// <summary>
  36.         /// Default constructor.
  37.         /// </summary>
  38.         public StreamerPost()
  39.         {
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Constructor that accepts a url as a parameter
  44.         /// </summary>
  45.         /// <param name="url">The url where the post will be submitted to.</param>
  46.         public StreamerPost(string url)
  47.             : this()
  48.         {
  49.             m_url = url;
  50.         }
  51.  
  52.         /// <summary>
  53.         /// Constructor allowing the setting of the url and items to post.
  54.         /// </summary>
  55.         /// <param name="url">the url for the post.</param>
  56.         /// <param name="values">The values for the post.</param>
  57.         public StreamerPost(string url, NameValueCollection values)
  58.             : this(url)
  59.         {
  60.             m_values = values;
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Gets or sets the url to submit the post to.
  65.         /// </summary>
  66.         public string Url
  67.         {
  68.             get
  69.             {
  70.                 return m_url;
  71.             }
  72.             set
  73.             {
  74.                 m_url = value;
  75.             }
  76.         }
  77.         /// <summary>
  78.         /// Gets or sets the name value collection of items to post.
  79.         /// </summary>
  80.         public NameValueCollection PostItems
  81.         {
  82.             get
  83.             {
  84.                 return m_values;
  85.             }
  86.             set
  87.             {
  88.                 m_values = value;
  89.             }
  90.         }
  91.         /// <summary>
  92.         /// Gets or sets the type of action to perform against the url.
  93.         /// </summary>
  94.         public PostTypeEnum Type
  95.         {
  96.             get
  97.             {
  98.                 return m_type;
  99.             }
  100.             set
  101.             {
  102.                 m_type = value;
  103.             }
  104.         }
  105.         /// <summary>
  106.         /// Posts the supplied data to specified url.
  107.         /// </summary>
  108.         /// <returns>a string containing the result of the post.</returns>
  109.         public string Post()
  110.         {
  111.             StringBuilder parameters = new StringBuilder();
  112.             for (int i = 0; i < m_values.Count; i++)
  113.             {
  114.                 EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
  115.             }
  116.             string result = PostData(m_url, parameters.ToString());
  117.             return result;
  118.         }
  119.         /// <summary>
  120.         /// Posts the supplied data to specified url.
  121.         /// </summary>
  122.         /// <param name="url">The url to post to.</param>
  123.         /// <returns>a string containing the result of the post.</returns>
  124.         public string Post(string url)
  125.         {
  126.             m_url = url;
  127.             return this.Post();
  128.         }
  129.         /// <summary>
  130.         /// Posts the supplied data to specified url.
  131.         /// </summary>
  132.         /// <param name="url">The url to post to.</param>
  133.         /// <param name="values">The values to post.</param>
  134.         /// <returns>a string containing the result of the post.</returns>
  135.         public string Post(string url, NameValueCollection values)
  136.         {
  137.             m_values = values;
  138.             return this.Post(url);
  139.         }
  140.         /// <summary>
  141.         /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
  142.         /// </summary>
  143.         /// <param name="postData">The data to post.</param>
  144.         /// <param name="url">the url to post to.</param>
  145.         /// <returns>Returns the result of the post.</returns>
  146.         private string PostData(string url, string postData)
  147.         {
  148.             HttpWebRequest request = null;
  149.             if (m_type == PostTypeEnum.Post)
  150.             {
  151.                 Uri uri = new Uri(url);
  152.                 request = (HttpWebRequest)WebRequest.Create(uri);
  153.                 request.Method = "POST";
  154.                 request.ContentType = "application/x-www-form-urlencoded";
  155.                 request.ContentLength = postData.Length;
  156.                 using (Stream writeStream = request.GetRequestStream())
  157.                 {
  158.                     UTF8Encoding encoding = new UTF8Encoding();
  159.                     byte[] bytes = encoding.GetBytes(postData);
  160.                     writeStream.Write(bytes, 0, bytes.Length);
  161.                 }
  162.             }
  163.             else
  164.             {
  165.                 Uri uri = new Uri(url + "?" + postData);
  166.                 request = (HttpWebRequest)WebRequest.Create(uri);
  167.                 request.Method = "GET";
  168.             }
  169.             string result = string.Empty;
  170.             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  171.             {
  172.                 using (Stream responseStream = response.GetResponseStream())
  173.                 {
  174.                     using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
  175.                     {
  176.                         result = readStream.ReadToEnd();
  177.                     }
  178.                 }
  179.             }
  180.             return result;
  181.         }
  182.         /// <summary>
  183.         /// Encodes an item and ads it to the string.
  184.         /// </summary>
  185.         /// <param name="baseRequest">The previously encoded data.</param>
  186.         /// <param name="dataItem">The data to encode.</param>
  187.         /// <returns>A string containing the old data and the previously encoded data.</returns>
  188.         private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
  189.         {
  190.             if (baseRequest == null)
  191.             {
  192.                 baseRequest = new StringBuilder();
  193.             }
  194.             if (baseRequest.Length != 0)
  195.             {
  196.                 baseRequest.Append("&");
  197.             }
  198.             baseRequest.Append(key);
  199.             baseRequest.Append("=");
  200.             baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
  201.         }
  202.     }
  203.  
  204. }
  205.